package cn.quevo.ucenter.client.util;

import cn.quevo.core.entity.BaseEntity;
import cn.quevo.core.entity.DoEntity;
import cn.quevo.core.entity.UserInfoDetails;
import cn.quevo.core.enums.ResultStatusEnum;
import cn.quevo.core.exception.CheckParamsException;
import cn.quevo.core.exception.NotLoginException;
import cn.quevo.core.lang.JSONUtils;
import cn.quevo.core.lang.StringUtils;
import cn.quevo.core.util.BeanUtils;
import cn.quevo.spring.util.PropertiesUtils;
import cn.quevo.spring.util.WebUtils;
import cn.quevo.ucenter.core.OAuthContext;
import cn.quevo.ucenter.core.entity.User;

import java.util.Map;

public class ClientUserUtil {

    private final static PropertiesUtils propertiesUtils = new PropertiesUtils("/application.properties");

    /**
     * 获取当前登录用户
     *
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018年3月28日 下午4:31:29
     */
    public static <T extends UserInfoDetails> T getLoginUser() {
        Object obj = WebUtils.getHttpSession(StringUtils.isBlank(getSessionType()) ? OAuthContext.OAUTH_USER : (OAuthContext.OAUTH_USER + "_" + getSessionType()));
        if (obj == null) {
            return null;
        }
        if (obj instanceof UserInfoDetails) {
            return (T) obj;
        }
        if (obj instanceof Map) {
            User user = new User();
            BeanUtils.mapToObject(user, (Map<String, Object>) obj);
            return (T) user;
        }
        if (StringUtils.isBlank(obj)) {
            return null;
        }
        try {
            return (T)JSONUtils.parse(StringUtils.valueOf(obj),User.class);
        } catch (Exception e) {
            User user = new User();
            BeanUtils.mapToObject(user, (Map<String, Object>) obj);
            return (T) user;
        }
    }

    /**
     * 设置当前登录用户信息
     *
     * @param obj
     */
    public static void setLoginUser(Object obj) {
        WebUtils.setHttpSession(StringUtils.isBlank(getSessionType()) ? OAuthContext.OAUTH_USER : (OAuthContext.OAUTH_USER + "_" + getSessionType()), obj);
    }

    /**
     * 初始化DoEntity
     *
     * @param entity
     * @author July july_sky@foxmail.com
     * @date 2018年3月29日 上午11:40:27
     */
    public static void init(DoEntity entity) {
        UserInfoDetails user = getLoginUser();
        if (user == null) {
            return;
        }
        entity.setOperatorId(user.getSubjectId());
        entity.setRecordOwnerId(user.getRecordOwnerId());
        entity.setCompId(user.getCompId());
    }

    /**
     * 初始化查询Bean
     *
     * @param entity
     * @author July july_sky@foxmail.com
     * @date 2018年3月29日 上午11:40:36
     */
    public static void init(BaseEntity entity) {
        UserInfoDetails user = getLoginUser();
        if (user == null) {
            return;
        }
        entity.setRecordOwnerId(user.getRecordOwnerId());
        entity.setOperatorId(user.getSubjectId());
        entity.setCompId(user.getCompId());
    }

    /**
     * 初始化BEAN
     *
     * @param entity
     */
    public static void init(BaseEntity entity, Long id) {
        if (StringUtils.isBlank(id)) {
            throw new CheckParamsException(ResultStatusEnum.RECORD_UNSELECTED);
        }
        UserInfoDetails user = getLoginUser();
        if (user == null) {
            throw new NotLoginException();
        }
        entity.setId(id);
        entity.setRecordOwnerId(user.getRecordOwnerId());
        entity.setOperatorId(user.getSubjectId());
        entity.setCompId(user.getCompId());
    }

    /**
     * 是否已登录
     *
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-04-03 11:42:27
     */
    public static boolean isLogin() {
        return getLoginUser() != null;
    }

    /**
     * 获取Session存储类型
     *
     * @return
     */
    public static String getSessionType() {
        String type = propertiesUtils.getProperties("spring.ucenter.sessionType", "");
        if (StringUtils.isBlank(type)) {
            type = propertiesUtils.getProperties("spring.ucenter.session-type", "");
        }
        return StringUtils.isBlank(type) || "none".equalsIgnoreCase(type) ? "" : type;
    }
}
